package com.android.nextBus.JSON;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.nextBus.DB.StopItem;

public class SingleStopParser {
	
	/**
	 * Parses data about a single stop returned from the server's single stop service
	 * into a StopItem
	 * @param data the json object returned from the Single Stop Service
	 * @param stopID the id of the stop for the data
	 * @return a Stop Item
	 */
	public static StopItem parse(String data, int stopID) {
		
		StopItem stop = null;
		try {
			JSONObject obj = new JSONObject(data);
			
			int lon = obj.getInt("x");		// note: the server returns lat/lon as integers, 
			int lat = obj.getInt("y");		// already converted for GeoPoint use
			String desc = obj.getString("d");
			String name = obj.getString("n");
				
			stop = new StopItem(name, desc, lon, lat, stopID);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return stop;
	}
}
